package com.example.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.LeaveInfo;
import com.example.demo.entity.User;
import com.example.demo.service.LeaveService;
import com.example.demo.util.SessionUtil;

@Controller
public class LeaveController {

	private static Logger logger = LoggerFactory.getLogger(LeaveController.class);
	@Autowired
	private LeaveService leaveService;
	
	
	
	/**
	 * 发起申请，新增信息
	 * @param msg
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/addLeaveInfo") 
	public String addLeaveInfo(String userId,String msg,HttpServletRequest request,Model model) {
		leaveService.addLeaveAInfo(userId,msg);
		return "新增成功...";
	}
	
	@RequestMapping("/getLeaveInfo") 
	public String getLeaveInfo(Model model) {
		
		User user = SessionUtil.get();
		
		List<LeaveInfo> list = leaveService.getLeaveInfoByUserId(user.getId());
		
		LeaveInfo leave = null;
		if(!ObjectUtils.isEmpty(list)){
			leave = list.get(0);
			model.addAttribute("id", leave.getId());
			model.addAttribute("msg", leave.getLeaveMsg());
			model.addAttribute("days", leave.getDays());
			model.addAttribute("userId", leave.getUserId());
			model.addAttribute("status", leave.getStatus());
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		return "leave";
	}
	
}
